package io.gitee.kotle.android.library.banner.impl.banner

import android.graphics.Rect
import android.view.ViewGroup
import android.view.animation.Interpolator
import androidx.lifecycle.LifecycleOwner
import androidx.viewpager2.widget.ViewPager2
import io.gitee.kotle.android.library.banner.IBanner

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/12/13 4:13 下午
 *
 */
open class DefaultBannerImpl<Data : IBanner.BannerData>(
    containerView: ViewGroup,
    lifecycleOwner: LifecycleOwner?
) : IBanner<Data> {
    private val helper = BannerViewHelper<Data>(containerView, lifecycleOwner)

    override val viewPager2: ViewPager2
        get() = helper.viewPage

    override fun setListener(listener: IBanner.Listener<Data>?) {
        helper.listener = listener
    }

    override fun setItemMargin(rect: Rect) {
        helper.itemMargin = rect
    }

    override fun setItemPadding(rect: Rect) {
        helper.itemPadding = rect
    }

    override fun setDatas(
        datas: List<Data>?,
        layoutId: Int?,
        isCanInfiniteScroll: Boolean,
        startIndex: Int
    ) {
        helper.repeatCount = if (isCanInfiniteScroll) {
            -1
        } else {
            helper.repeatCount
        }
        helper.customItemLayoutRes = layoutId
        helper.setDatas(datas, startIndex)
    }

    override fun getDatas(): MutableList<Data>? {
        return helper.adapter.currentList as MutableList<Data>
    }

    override fun setCanAutoScroll(enable: Boolean) {
        helper.enableAutoScroll = enable
    }

    override fun setRepeatCount(repeatCount: Int) {
        helper.repeatCount = repeatCount
    }

    override fun setAutoScrollDuration(duration: Int) {
        helper.animDuration = duration.toLong()
    }

    override fun setAutoScrollInterval(time: Long) {
        helper.stayDuration = time
    }

    override fun startAutoScroll() {
        setCanAutoScroll(true)
    }

    override fun stopAutoScroll() {
        setCanAutoScroll(false)
    }

    override fun setPageTransformer(transformer: ViewPager2.PageTransformer?) {
        viewPager2.setPageTransformer(transformer)
    }

    override fun canOnlyClickCurrentItem(onlyClick: Boolean) {
        helper.canOnlyClickCurrentItem = onlyClick
    }

    override fun setLifecycle(lifecycleOwner: LifecycleOwner?) {
        helper.setLifecycle(lifecycleOwner)
    }

    override fun setAnimInterpolator(interpolator: Interpolator) {
        helper.animInterpolator = interpolator
    }
}